<template>
  <el-dialog
      modal-value="dialogVisible"
      :title="dialogTitle"
      style="padding: 40px"
      width="600px"
      @close="handleClose"
      draggable
      overflow
  >
    <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-width="100px"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="form.name"/>
      </el-form-item>

      <el-form-item label="权限字符" prop="code">
        <el-input v-model="form.code"/>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark" type="textarea" :rows="4"/>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="handleConfirm">确 认</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {ref, watch} from 'vue'
import requestUtil from "@/utils/request";
import {ElMessage} from "element-plus";

const props = defineProps({
  id: {
    type: Number,
    default: -1,
    required: true
  },
  dialogTitle: {
    type: String,
    default: '',
    required: true
  },
  dialogVisible: {
    type: Boolean,
    default: false,
    required: true
  }
})

const form = ref({
  id: -1,
  name: '',
  code: '',
  remark: '',
})

const rules = ref({
  name: [
    {required: true, message: '请输入角色名称'}
  ],
  code: [
    {required: true, message: '请输入权限字符'}
  ],
})

const formRef = ref(null)
const initFormData = async (id) => {
  const res = await requestUtil.get('role/action?id=' + id);
  form.value = res.data.msg;
}

watch(
    () => props.dialogVisible,
    () => {
      let id = props.id;
      if (id !== -1) {
        initFormData(id)
      } else {
        form.value = {
          id: -1,
          name: '',
          code: '',
          remark: '',
        }
      }
    }
)

const emits = defineEmits(['update:modelValue', 'initRoleList'])

const handleClose = () => {
  formRef.value.resetFields();
  emits('update:modelValue', false)
}
const handleConfirm = () => {
  formRef.value.validate(async (valid) => {
    if (valid) {
      let res = await requestUtil.post('role/save', form.value);
      let data = res.data;
      if (data.code === 200) {
        ElMessage.success('执行成功')
        formRef.value.resetFields();
        emits('initRoleList');
        handleClose();
      } else {
        ElMessage.error(data.msg);
      }
    } else {
      console.log('fail')
    }
  })
}
</script>
